# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html


# useful for handling different item types with a single interface
import csv
import os

from itemadapter import ItemAdapter


class TitleCreatePipeline:
    def open_spider(self, spider):
        self.save_path = os.getcwd() + "/青岛问政/"
        if not os.path.exists(self.save_path):  # 判断文件夹是否存在
            os.makedirs(self.save_path)  # 没有就创建文件夹

    def process_item(self, item, spider):
        type_ = item.get("type")
        if type_ == "title":
            file_name = item.get("name")
            f = open(self.save_path + file_name + ".csv", "w")
            f.close()

        return item


class FirstprojectPipeline:
    def process_item(self, item, spider):
        self.save_path = os.getcwd() + "/青岛问政/"
        type_ = item.get("type")
        if type_ == "info":
            file_name = item.get("name")
            with open(self.save_path + file_name + ".csv", "a") as f:
                # 创建一个csv的DictWriter对象，这样才能够将写入csv格式数据到这个文件
                f_csv = csv.DictWriter(f, ['number', 'desc', 'ask_time', 'ans_time'])
                # 写入多行行（当做数据）
                item.pop("type")  # 删除type 这个key-value
                item.pop("name")  # name 这个key-value
                f_csv.writerows([item])
                print("保存信息到%s.csv....ok" % file_name)

        return item
